$(document).ready(function () {
    let op = {
        "ajax": {
            "url": "findAll",
            "dataSrc": "lstDto",
            "data": function (d) {
                d.typeName = $('#txtTypeName').val();
                d.methodName = $('#txtMethodName').val();
                d.threadId = $('#txtThreadId').val();
                d.createDate = $('#txtCreateDate').val();
            }
        },
        "paging": false,
        'searching': false,
        "columns": [
            {"data": "seq"},
            {"data": "typeName"},
            {"data": "methodName"},
            {"data": "lineNo"},
            {"data": "totalTime"},
            {"data": "threadId"},
            {"data": "createDate"}
        ]
    };

    let dt;

    reload();

    $("#btnOpenMonitor").click(function () {
        $.ajax({
            method: "get",
            url: "findPidAndScript", success: function (result) {
                if (result == null || result == undefined) {
                    alert("操作异常。");
                    return;
                }

                let pidList = [];

                let scriptNameList = [];

                if (result.flag === 'SUCCESS') {
                    $('#selectPidAndScriptDialog').dialog({
                        title: '请选择需要监控的进程和要执行的Btrace脚本',
                        width: 900,
                        height: 650,
                        showClose: false,
                        open: function (dialog) {
                            pidList = result.obj.pidList;
                            scriptNameList = result.obj.scriptNameList;


                            if (pidList != null && pidList != undefined && pidList.length > 0) {
                                let htmlTemp = "";
                                for (let i = 0; i < pidList.length; i++) {
                                    htmlTemp += "<tr><td style='border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;'><input type='radio' name='radioPid' value='" + pidList[i] + "'/></td><td style='border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;text-align: left'>" + pidList[i] + "</td></tr>";
                                }
                                $("#pidTbody").html(htmlTemp);
                            }

                            if (scriptNameList != null && scriptNameList != undefined && scriptNameList.length > 0) {
                                let htmlTemp = "";
                                for (let i = 0; i < scriptNameList.length; i++) {
                                    htmlTemp += "<tr><td style='border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;'><input type='checkbox' name='checkBoxScript' value='" + scriptNameList[i] + "'/></td><td style='border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;text-align: left'>" + scriptNameList[i] + "</td></tr>";
                                }
                                $("#scriptTbody").html(htmlTemp);
                            }

                            $('#cancel').on('click', function () {
                                dialog.close();
                                closeMonitor(false);
                            })
                        },
                        enterBtn: {
                            cb: function (dialog) {
                                let radioPidVal = $("input[name='radioPid']:checked").val();
                                if (radioPidVal == null || radioPidVal == undefined || radioPidVal == "") {
                                    alert("请选择进程。");
                                    return;
                                }

                                let checkBoxScriptVal = [];
                                let checkBoxScriptValTemp = $("input[name='checkBoxScript']:checked").val();
                                if (checkBoxScriptValTemp == null || checkBoxScriptValTemp == undefined || checkBoxScriptValTemp == "") {
                                    alert("请选择脚本。");
                                    return;
                                }
                                checkBoxScriptVal.push(checkBoxScriptValTemp);

                                let d = {pidStr: radioPidVal, scriptNameList: checkBoxScriptVal};
                                $.ajax({
                                    method: "post",
                                    url: "openMonitor",
                                    data: JSON.stringify(d),
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    success: function (result) {
                                        if (result == null || result == undefined || result == '' || result.flag != 'SUCCESS') {
                                            alert("操作异常。");
                                            return;
                                        }

                                        if (result.flag == 'SUCCESS') {
                                            alert("操作成功。");
                                            dialog.close()
                                        } else {
                                            alert(result.message);
                                        }

                                    }
                                });
                            }
                        },
                        cancelBtn: {
                            cb: function (dialog) {
                                dialog.close()
                                closeMonitor(false);
                            }
                        }
                    })
                } else {
                    alert(result.message);
                }
            }
        });
    });


    // 解析文本文件日志，并保存日志到数据库
    $("#btnSaveAllLog").click(function () {
        $.ajax({
            method: "post",
            url: "save", success: function (result) {
                if (result == null || result == undefined) {
                    alert("操作异常。");
                    return;
                }

                if (result.flag === 'SUCCESS') {
                    alert("操作成功。");
                } else {
                    alert(result.message);
                }

                reload();
            }
        });
    });

    $("#btnDeleteAllLog").click(function () {
        $.ajax({
            method: "post",
            url: "deleteAllLog", success: function (result) {
                if (result == null || result == undefined) {
                    alert("操作异常。");
                    return;
                }

                if (result.flag === 'SUCCESS') {
                    alert("操作成功。");
                } else {
                    alert(result.message);
                }

                reload();
            }
        });
    });

    $("#btnCloseMonitor").click(function () {
        closeMonitor(true);
    });

    $("#btnReload").click(function () {
        reload();
    });

    function reload() {
        if (dt != null) {
            dt.destroy();
        }

        dt = $('#example').DataTable(op);
    }

    function closeMonitor(showMsg) {
        $.ajax({
            method: "post",
            url: "closeMonitor", success: function (result) {
                if (!showMsg) {
                    console.dir(result);
                    return;
                }

                if (result == null || result == undefined) {
                    alert("操作异常。");
                    return;
                }

                alert(result.message);
            }
        });
    }
});
